package com.css.bjdt_report.annual.controller;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.css.bjdt_report.annual.entity.RptAnnual;
import com.css.bjdt_report.annual.service.AnnualService;
import com.css.bjdt_report.util.PropertyFilter;

//报表版本维护Controller
@Controller
@RequestMapping(value = "/rptAnnualController")
public class AnnualController {

	@Autowired
	private AnnualService annualService;

	@RequestMapping("/list")
	public String list() {
		return "report-annual/report-annual-list";
	}

	//查询报表模板，并生成LIGERUI格式的json
	@ResponseBody
	@RequestMapping("/tableData")
	public Map tableData(HttpServletRequest request, @RequestParam(value = "pageNum") int pageNum,
			@RequestParam(value = "pageSize") int pageSize, @RequestParam(value = "sortname") String sortname,
			@RequestParam(value = "sortorder") String sortorder) {
		Criteria cnd = PropertyFilter.cri(request);

		//cnd.where().and("SYSTEMID", "=", "1").and("NODEID", "=", "1");

		cnd.getOrderBy().desc("ORDERID");

		return annualService.queryPage(cnd, pageNum, pageSize);
	}

	@RequestMapping("/preCreate")
	public String preCreate() {
		return "report-annual/report-annual-input";
	}

	//保存报表模板
	@RequestMapping(value = "/insertAnnual", method = RequestMethod.POST)
	public String insertAnnual(RptAnnual rptAnnual, Model model) {
		annualService.insert(rptAnnual);
		model.addAttribute("message", "操作成功");
		return "report-annual/report-annual-input";
	}

	@RequestMapping(value = "/preUpdate/{annualId}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String annualId, Model model) {
		model.addAttribute(annualService.queryByID(annualId));
		return "report-annual/report-annual-input";
	}

	//修改报表模板
	@RequestMapping("/updateAnnual")
	public String updateAnnual(RptAnnual rptAnnual, Model model) {
		annualService.update(rptAnnual);
		model.addAttribute("message", "操作成功");
		return "report-annual/report-annual-input";
	}

	//报表模板明细
	@RequestMapping(value = "/detail/{annualId}", method = RequestMethod.GET)
	public String detail(@PathVariable String annualId, Model model) {
		model.addAttribute(annualService.queryByID(annualId));
		return "report-annual/report-annual-detail";
	}

	//删除一个报表模板
	@ResponseBody
	@RequestMapping(value = "/deleteAnnual/{annualId}")
	public String deleteAnnual(@PathVariable String annualId) {
		annualService.deleteAnnual(annualId);
		return "success";
	}

	//报表管理中得到所有报表模板
	@ResponseBody
	@RequestMapping(value = "/queryAllAnnualSelect", method = RequestMethod.POST)
	public List<RptAnnual> queryAllAnnualSelect() {
		return annualService.queryAllAnnual();
	}

	//判断报表版本的ID是否存在
	@ResponseBody
	@RequestMapping(value = "/isAnnualIdHave/{rptAttributeId}", method = RequestMethod.POST)
	public String isAnnualIdHave(@PathVariable String rptAttributeId) {
		Calendar calendar = Calendar.getInstance();
		int thisYear = calendar.get(Calendar.YEAR);
		String annualId = "";
		if (rptAttributeId.equals("1")) {
			//半年报
			annualId = annualService.queryThisYearCount(thisYear + "", true);
		} else {
			//全年报
			annualId = annualService.queryThisYearCount(thisYear + "", false);
		}

		if (annualId.equals("0")) {
			return "0";
		} else {
			return "1";
		}
	}
}
